<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 1.移除数组 [1, 2, 3, 4, 2] 中的2。不要直接修改数组，结果返回新的数组
      let arr1 = [1, 2, 3, 4, 2];
      let arr2 = []
      arr1.forEach(function(e){
        if(e !== 2){
          arr2.push(e)
        }
      })
      console.log(arr2);

      // 2.定义一个函数,能在数组 [1, 2, 3, 4, 2] 的 "2"后面添加元素 "JavaScript"。不要直接修改数组 arr，结果返回新的数组
        let arr = [1, 2, 3, 4, 2];
        let arr3 = arr;
        function fn(){
          arr3.splice(1,1,"JavaScript");
          arr3.splice(-1,1,"JavaScript");
          return arr3;
        }
        fn();
        console.log('arr3',arr3)

      // 3.统计数组 [1, 2, 3, 4, 2] 中2出现的次数
        let arr4 = [1, 2, 3, 4, 2];
        let sum = 0;
        
        arr4.forEach(function(e){
          if(e===2){
            sum+=1;
          }
        })
        console.log(sum);

      // 4.找出数组 [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"] 中重复出现过的元素,并用数组将重复元素装起来输出
        let arr6 = [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"];
        let temp = [];
        arr6.forEach(function(e){
          if(arr6.indexOf(e)!==arr.lastIndexOf(e)){
            temp.push(e)
          }
        })
        console.log('temp',temp)
      
      // 5.在数组里面输出年龄小于17的对象
      let arr5 = [
        {
          name: '111',
          sex: 'boy',
          age: 18,
        },
        {
          name: '222',
          sex: 'girl',
          age: 17,
        },
        {
          name: '333',
          sex: 'boy',
          age: 16,
        },
        {
          name: '444',
          sex: 'girl',
          age: 15,
        },
        {
          name: '555',
          sex: 'boy',
          age: 20,
        },
      ];
        let obj = [];
        
        arr5.forEach(function(e){
          if(e.age<=17){
            obj.push(e);
          }
        })
        console.log(obj);
      // 6. 数组扁平化, 把数组[1, [2, [3, 4, 5]]]转成[1, 2, 3, 4，5]
        let arr7=[1, [2, [3, 4, 5]]];
        let arr8 = []
        let arr9 = []
        arr8.push(arr7.join())
        arr9.push(arr8.join())
        arr9.forEach(function(e){
          
          Number(e)
        })
        
        console.log(arr9);

      // 7. 使用reduce实现[1,2,3,4,9]数组中所有成员的和
        let arr10 = [1,2,3,4,9];
        let result = arr10.reduce((sum,n)=>sum+n,0)
        console.log(result);

      // 8. 数组去重的方法 (4种)

      // 1双重for循环去重
      // 2.对象去重
      // 3.Set去重
      // 4.includes去重
    </script>
  </body>
</html>
